﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NUS.ISS.ARS.Web.PageController;
using NUS.ISS.ARS.Web.UserControls;
using NUS.ISS.ARS.BusinessEntity;

namespace NUS.ISS.ARS.Web.Booking
{
    public partial class PassengerInfo : BookingBaseController
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            CurrentPage = "PassengerInfo.aspx";
            base.Page_Load(sender, e);

            if (!Page.IsPostBack)
            {
                btnPre.Visible = IsPrePageVisible;
                btnNext.Visible = IsNextPageVisible;
                InitialPassgenerUserControl();
            }
        }

        private void InitialPassgenerUserControl()
        {
            if (ReservationData.Passengers == null ||
               ReservationData.Passengers.Count == 0)
            {
                RepeaterPassengerIno.DataSource = new List<Passenger>[ReservationData.NumOfAdult + ReservationData.NumOfChild];
                RepeaterPassengerIno.DataBind();
            }
            else
            {
                RepeaterPassengerIno.DataSource = new List<Passenger>[ReservationData.NumOfAdult + ReservationData.NumOfChild];
                RepeaterPassengerIno.DataBind();
                ReadFromSessionToRepeater();
            }
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            SaveToSession();
            MoveToNextPage();
        }

        
        protected void btnPre_Click(object sender, EventArgs e)
        {
            MoveToPrePage();
        }

        protected void RepeaterPassengerIno_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item
                || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var userControl = (PassengerInfoUserControl)e.Item.FindControl("PassengerInfoUserControl");
                userControl.Countries = GetAllCountries().ToList();
            }
        }

        private void SaveToSession()
        {
            ReservationData.Passengers = new List<Passenger>();
            foreach (RepeaterItem item in RepeaterPassengerIno.Items)
            {
                var userControl = (PassengerInfoUserControl)item.FindControl("PassengerInfoUserControl");
                ReservationData.Passengers.Add(new Passenger()
                {
                    Type = (byte)userControl.Type,
                    FirstName = userControl.SurName,
                    LastName = userControl.GivenName,
                    PassportNumber = userControl.PassportNumber,
                    DateOfBirth = userControl.DayOfBirth,
                    ExpiryDate = userControl.PassportExpiryDate,
                    CountryId=userControl.CountryId
                });
            }
        }

        private void ReadFromSessionToRepeater()
        {
            int passengerIdInList = 0;
            Passenger[] passengerArr = ReservationData.Passengers.ToArray() ;

            foreach (RepeaterItem item in RepeaterPassengerIno.Items)
            {
                var userControl = (PassengerInfoUserControl)item.FindControl("PassengerInfoUserControl");
                userControl.Type = passengerArr[passengerIdInList].Type;
                userControl.SurName = passengerArr[passengerIdInList].FirstName;
                userControl.GivenName = passengerArr[passengerIdInList].LastName;
                userControl.PassportNumber = passengerArr[passengerIdInList].PassportNumber;
                userControl.DayOfBirth = passengerArr[passengerIdInList].DateOfBirth;
                userControl.PassportExpiryDate = passengerArr[passengerIdInList].ExpiryDate;
                userControl.CountryId = passengerArr[passengerIdInList].CountryId;
                passengerIdInList++;
            }
        }
    }
}